#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
配置模块使用示例
"""

import os
import sys
# 添加项目根目录到Python搜索路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from config.config import config
from vector_store.factory import VectorStoreFactory
from graph.graph_connection import NebulaGraphConnection

def display_config():
    """显示当前配置信息"""
    print("=== 当前配置信息 ===")
    
    # NebulaGraph配置
    nebula_config = config.get_nebula_config()
    print("\n== NebulaGraph配置 ==")
    print(f"IP地址: {nebula_config['ip']}")
    print(f"端口: {nebula_config['port']}")
    print(f"用户名: {nebula_config['user']}")
    print(f"密码: {'*' * len(nebula_config['password'])}")
    print(f"工作空间: {nebula_config['space']}")
    
    # 向量存储配置
    print(f"\n== 向量存储配置 ({config.vector_store_type}) ==")
    vector_store_config = config.get_vector_store_config()
    if config.vector_store_type == "sqlite":
        print(f"数据库路径: {vector_store_config['db_path']}")
    elif config.vector_store_type == "milvus":
        if vector_store_config.get("uri"):
            print(f"URI: {vector_store_config['uri']}")
            print(f"Token: {vector_store_config.get('token', '未设置')}")
        else:
            print(f"数据库路径: {vector_store_config['db_path']}")
        print(f"集合名称: {vector_store_config['collection_name']}")
        print(f"向量维度: {vector_store_config['dimension']}")
    
    # 嵌入模型配置
    embedding_config = config.get_embedding_config()
    print("\n== 嵌入模型配置 ==")
    print(f"模型名称: {embedding_config['model_name']}")
    print(f"API密钥: {embedding_config['api_key'][:5]}...{embedding_config['api_key'][-5:]}")
    print(f"API基础URL: {embedding_config['api_base']}")
    
    # LLM配置
    llm_config = config.get_llm_config()
    print("\n== LLM配置 ==")
    print(f"模型名称: {llm_config['model']}")
    print(f"API密钥: {llm_config['api_key'][:5]}...{llm_config['api_key'][-5:]}")
    print(f"API基础URL: {llm_config['api_base']}")
    print("\n===================")

def demo_connect_graph():
    """演示连接图数据库"""
    print("\n演示连接图数据库...")
    try:
        # 获取NebulaGraph配置
        nebula_config = config.get_nebula_config()
        
        # 创建图数据库连接
        graph_connection = NebulaGraphConnection(nebula_config)
        connected = graph_connection.connect()
        
        if connected:
            print("✓ 图数据库连接成功！")
            
            # 执行一个简单查询
            success, result = graph_connection.execute_query("SHOW TAGS")
            if success:
                print("查询结果:")
                print(result)
            
            # 关闭连接
            graph_connection.close()
        else:
            print("✗ 图数据库连接失败！")
    except Exception as e:
        print(f"✗ 图数据库连接出错: {str(e)}")

def demo_create_vector_store():
    """演示创建向量存储"""
    print("\n演示创建向量存储...")
    try:
        # 获取向量存储配置
        vector_store_type = config.vector_store_type
        vector_store_config = config.get_vector_store_config()
        
        # 使用工厂创建向量存储
        vector_store = VectorStoreFactory.create_vector_store(
            store_type=vector_store_type,
            config=vector_store_config
        )
        
        if vector_store:
            print(f"✓ {vector_store_type.upper()}向量存储创建成功！")
            
            # 关闭连接
            vector_store.close()
        else:
            print(f"✗ {vector_store_type.upper()}向量存储创建失败！")
    except Exception as e:
        print(f"✗ 向量存储创建出错: {str(e)}")

if __name__ == "__main__":
    # 显示当前配置
    display_config()
    
    # 运行示例
    demo_connect_graph()
    demo_create_vector_store() 